package com.xinbochuang.machine.admin.service.impl.macRoom;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.MacGuard;
import com.xinbochuang.machine.admin.domain.macRoom.EquCabinet;
import com.xinbochuang.machine.admin.domain.macRoom.Equipment;
import com.xinbochuang.machine.admin.domain.macRoom.MacRoom;
import com.xinbochuang.machine.admin.domain.macRoom.Ulocation;
import com.xinbochuang.machine.admin.domain.system.SysDept;
import com.xinbochuang.machine.admin.domain.system.SysUser;
import com.xinbochuang.machine.admin.dto.MacRoomDto;
import com.xinbochuang.machine.admin.mapper.MacGuardMapper;
import com.xinbochuang.machine.admin.mapper.macRoom.EquCabinetMapper;
import com.xinbochuang.machine.admin.mapper.macRoom.EquipmentMapper;
import com.xinbochuang.machine.admin.mapper.macRoom.MacRoomMapper;
import com.xinbochuang.machine.admin.mapper.macRoom.UlocationMapper;
import com.xinbochuang.machine.admin.mapper.system.SysDeptMapper;
import com.xinbochuang.machine.admin.mapper.system.SysUserMapper;
import com.xinbochuang.machine.admin.service.macRoom.IMacRoomService;
import com.xinbochuang.machine.admin.service.system.ISysDeptService;
import com.xinbochuang.machine.common.utils.SecurityUtils;
import com.xinbochuang.machine.common.utils.ServletUtils;
import com.xinbochuang.machine.common.utils.StringUtils;
import com.xinbochuang.machine.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 杜文博
 * @date 2020-09-08
 */
@Service
public class MacRoomServiceImpl extends ServiceImpl<MacRoomMapper, MacRoom> implements IMacRoomService {

    @Autowired
    private MacRoomMapper macRoomMapper;

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private EquCabinetMapper equCabinetMapper;

    @Autowired
    private UlocationMapper ulocationMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private MacGuardMapper macGuardMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Resource
    private ISysDeptService deptService;

    @Override
    public IPage<MacRoom> findMacRoomByNameForPage(MacRoom macRoom) {
        List<Integer> deptIds = deptService.getDeptIdsByDataScope();
        Page<MacRoom> page = new Page<>(macRoom.getPageNum(), macRoom.getPageSize());
        return macRoomMapper.getMacRoomList(page, macRoom, deptIds);
    }

    @Override
    public List<MacRoom> findMacRoomByName(String macName) {
        List<MacRoom> list = macRoomMapper.findMacRoom(macName);
        return list;
    }

    @Override
    public List<MacRoom> equAllList(MacRoom macRoom) {
        QueryWrapper<MacRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MacRoom::getDelteFlag, 0);
        queryWrapper.lambda().orderByAsc(MacRoom::getCreateTime);
        return macRoomMapper.selectList(queryWrapper);
    }

    @Override
    public int addMacPrincipal(MacRoom macRoom) {
        MacRoom result = new MacRoom();
        MacGuard macGuard = new MacGuard();
        result = macRoomMapper.selectById(macRoom.getUuid());
        if (StringUtils.isNotBlank(macRoom.getMacPrincipalId())) {
            //将安全员插入机房列表
            result.setMacPrincipal(macRoom.getMacPrincipal());
            result.setMacPrincipalId(macRoom.getMacPrincipalId());
            result.setMacPrincipalTel(macRoom.getMacPrincipalTel());
            result.setDeptId(macRoom.getDeptId());
            result.setDeptName(macRoom.getDeptName());
            //将安全员信息插入安全员列表
            String macGuardId = UUID.randomUUID().toString().trim().replaceAll("-", "");
            macGuard.setUuid(macGuardId);
            macGuard.setCreateBy(SecurityUtils.getUsername());
            macGuard.setUpdateBy(SecurityUtils.getUsername());
            macGuard.setCreateTime(DateUtil.now());
            macGuard.setUpdateTime(DateUtil.now());
            macGuard.setMacId(macRoom.getUuid());
            macGuard.setUserId(macRoom.getMacPrincipalId());
            macGuard.setUserName(macRoom.getMacPrincipal());
        }
        macGuardMapper.insert(macGuard);
        return macRoomMapper.updateById(result);
    }

    @Override
    public int addMacGuard(MacRoomDto macRoomDto) {
        String[] macGuardIdList = macRoomDto.getMacIds().split(",");
        Integer result = null;
        if (macGuardIdList.length != 0) {
            for (String str : macGuardIdList) {
                MacGuard macGuard = new MacGuard();
                MacRoom macRoom = new MacRoom();
                if (StringUtils.isNotBlank(macRoomDto.getMacPrincipalId())) {
                    //将安全员插入机房列表
                    macRoom = macRoomMapper.selectById(str);
                    macRoom.setMacPrincipal(macRoomDto.getMacPrincipal());
                    macRoom.setMacPrincipalId(macRoomDto.getMacPrincipalId());
                    macRoom.setMacPrincipalTel(macRoomDto.getMacPrincipalTel());
                    macRoom.setDeptId(macRoomDto.getDeptId());
                    macRoom.setDeptName(macRoomDto.getDeptName());
                    //将安全员信息插入安全员列表
                    String macGuardId = UUID.randomUUID().toString().trim().replaceAll("-", "");
                    macGuard.setUuid(macGuardId);
                    macGuard.setCreateBy(SecurityUtils.getUsername());
                    macGuard.setUpdateBy(SecurityUtils.getUsername());
                    macGuard.setCreateTime(DateUtil.now());
                    macGuard.setUpdateTime(DateUtil.now());
                    macGuard.setMacId(macRoom.getUuid());
                    macGuard.setUserId(macRoomDto.getMacPrincipalId());
                    macGuard.setUserName(macRoomDto.getMacPrincipal());
                    macGuardMapper.insert(macGuard);
                    result = macRoomMapper.updateById(macRoom);
                }

            }
        }

        return result;
    }

    @Override
    public MacRoom getMacDetail(String uuid) {
        return macRoomMapper.selectById(uuid);
    }

    @Override
    public List<SysUser> getMacGuard() {
        String userId = String.valueOf(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        SysUser sysUser = new SysUser();
        sysUser = sysUserMapper.selectById(userId);
        Integer deptId = sysUser.getDeptId();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getDelFlag, 0);
        queryWrapper.lambda().eq(SysUser::getDeptId, deptId);
        List<SysUser> sysUsers = sysUserMapper.selectList(queryWrapper);
        List<Integer> deptIds = new ArrayList<>();
        for (SysUser sysUser1 : sysUsers) {
            deptIds.add(sysUser1.getDeptId());
        }
        QueryWrapper<SysDept> sysDeptQueryWrapper = new QueryWrapper<>();
        sysDeptQueryWrapper.lambda().in(SysDept::getDeptId, deptIds);
        List<SysDept> sysDepts = sysDeptMapper.selectList(sysDeptQueryWrapper);
        for (SysUser sysUser1 : sysUsers) {
            for (SysDept sysDept : sysDepts) {
                if (sysUser1.getDeptId() == sysDept.getDeptId()) {
                    sysUser1.setDept(sysDept);
                }
            }
        }
        return sysUsers;
    }

    @Override
    public List<EquCabinet> finEquCabinetListByMacId(String macId) {
        return macRoomMapper.getEquCabinetByMacId(macId);
    }

    @Override
    public boolean updateByMacId(String macId) {
        MacRoom macRoom = new MacRoom();
        macRoom.setUuid(macId);
        macRoom.setDelteFlag(1);
        Integer i = macRoomMapper.updateById(macRoom);
        Boolean j = true;
        if (i == 1) {
            j = macRoomMapper.deleteCabinetByMacId(macId);
        }
        return j;
    }

    @Override
    public int insertMacroom(MacRoom macRoom) {
        return macRoomMapper.insert(macRoom);
    }

    @Override
    public int insertEquCabinet(EquCabinet equCabinet) {
        return equCabinetMapper.insert(equCabinet);
    }

    @Override
    public int inserEquipment(Equipment equipment) {
        return equipmentMapper.insert(equipment);
    }

    @Override
    public int delteMacroomById(String macRoomId) {
        Integer i = macRoomMapper.deleteById(macRoomId);
        List<EquCabinet> lists = macRoomMapper.getEquCabinetByMacId(macRoomId);
        if (lists.size() > 0) {
            for (int y = 0; y < lists.size(); y++) {
                macRoomMapper.deleteEquipmentByCabinetId(lists.get(y).getUuid());
                ulocationMapper.deleteUlocationByCabinetId(lists.get(y).getUuid());
            }
        }
        Integer j = null;
        if (i == 1) {
            j = macRoomMapper.deleteEquCabinetByMacId(macRoomId);
        }
        return j;
    }

    @Override
    public int insertUlocation(Ulocation ulocation) {
        return ulocationMapper.insert(ulocation);
    }
}
